package org.distributed.transaction.core.event;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.stereotype.Component;

/**
 * 事件广播器的默认实现
 */
@Component
public class DefaultTransactionEventMulticaster implements TransactionEventMulticaster {

	/**
	 * 存放事件监听器
	 */
	private static final List<TransactionListener> transactionListeners = new CopyOnWriteArrayList<TransactionListener>();

	@Override
	public void fireTransactionEvent(TransactionEvent transactionEvent) {
		for (TransactionListener transactionListener : transactionListeners) {
			transactionListener.onTransactionEvent(transactionEvent);
		}
	}

	@Override
	public void addTransactionListener(TransactionListener transactionListener) {
		transactionListeners.add(transactionListener);
	}

	@Override
	public void removeAllListeners() {
		transactionListeners.clear();
	}

	@Override
	public void removeTransactionListener(TransactionListener transactionListener) {
		transactionListeners.remove(transactionListener);
	}

}
